<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This class is representing a XML feed similar to this:
 * 
 * <activity:object>
 *   <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
 *   <content type="text">oh, what a lady</content>
 *   <link rel="alternate" type="text/html" href="http://thoughts.about.com/pamela/123456"></link>
 *   <id>http://thoughts.about.com/pamela/123456</id>
 * </activity:object>
 * 
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Activitystream_Object implements Atom_ChildElement {
    
    /**
     * @var Array of Atom_ChildElement
     */
    private $childElements = array();
    
    const ELEMENT_OBJECT = "object";
    
    public function addChildElement(Atom_ChildElement $childElement) {
        $this->childElements[] = $childElement;
    }
    
    /**
     * This method should produce something like this:
     * 
     * <activity:object>
     *   <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
     *   <content type="text">oh, what a lady</content>
     *   <link rel="alternate" type="text/html" href="http://thoughts.about.com/pamela/123456"></link>
     *   <id>http://thoughts.about.com/pamela/123456</id>
     * </activity:object>
     * 
     * @param DOMDocument$dom
     */
    public function asDomElement(DOMDocument $dom) {
        $element = $dom->createElement(ActivityStream::AS_NS_PREFIX . ':' . self::ELEMENT_OBJECT);
        foreach ($this->childElements as $childElement) {
            $element->appendChild($childElement->asDomElement($dom));
        }
        return $element;
    }
    
    public static function buildFromDOMElement(DOMElement $elm) {
        $as_obj = new Activitystream_Object();
        
        $as_obj->addChildElement(Activitystream_ObjectType::buildFromDOMElement($elm->getElementsByTagName(Activitystream_ObjectType::ELEMENT_OBJECTTYPE)->item(0)));

        // Google Buzz is not sending ID every time?
        $id = $elm->getElementsByTagName(Atom_Feed_Id::ELEMENT_ID)->item(0);
        if($id) {
            $as_obj->addChildElement(Atom_Entry_Id::buildFromDOMElement($id));
        } else {
            // Super hack
            $as_obj->addChildElement(new Atom_Entry_Id(time()));
        }
        
        $content = $elm->getElementsByTagName(Atom_Entry_Content::ELEMENT_CONTENT)->item(0);
        if($content == null) {
            $as_obj->addChildElement(new Atom_Entry_Content("No content.", Atom_Entry_Content::TYPE_HTML));
        } else {
            $as_obj->addChildElement(Atom_Entry_Content::buildFromDOMElement($content));
        }
        
        foreach ($elm->getElementsByTagName(Atom_Entry_Link::ELEMENT_LINK) as $elmLink) {
            
            if( $elmLink->parentNode->nodeName !== 'activity:object') {
                continue;
            }
            
            $as_obj->addChildElement(Atom_Entry_Link_AlternateHtml::buildFromDOMElement($elmLink));
        }

        return $as_obj;
    }
}
